<?php
//判断上传的文件是否出错,是的话，返回错误
if($_FILES["file"]["error"])
{
    // echo $_FILES["file"]["error"];
    $result['status'] = 'n';
    $result['info'] = '上传文件错误';
    exit(json_encode($result));
}
else
{
    $files = $_FILES["file"];
    $_FILES["file"]["type"] = explode(",", $_FILES["file"]["type"]);

    //没有出错
    //加限制条件
    //判断上传文件类型为png或jpg且大小不超过1024000B
    if((in_array('image/png', $_FILES["file"]["type"]) || in_array('image/jpg', $_FILES["file"]["type"]) || in_array('image/jpeg', $_FILES["file"]["type"])))
    {
        //防止文件名重复

        // $path = time().$_FILES["file"]["name"];
        $ext = strtolower(pathinfo($files['name'],PATHINFO_EXTENSION));
        $fileName = md5(uniqid(microtime(true),true)).'.'.$ext;
        $path = "uploads/shop/";
        $destName = $path.$fileName;
        //检查文件或目录是否存在
        if(file_exists("/uploads/shop/"))
        {
            // $info = "该文件已存在";
            $result['status'] = 'n';
            $result['info'] = '该文件已存在';
            exit(json_encode($result));
        }
        else
        {
            if (move_uploaded_file($files['tmp_name'],$destName)){
                // $info =  $destName;
                $result['status'] = 'y';
                $result['info'] = '上传成功';
                $result['src'] = $destName;
                exit(json_encode($result));
            }else{
                // $info =  "上传失败";
                $result['status'] = 'n';
                $result['info'] = '上传失败';
                exit(json_encode($result));
            }
        }
    }
    else
    {
        // $info = "文件类型不对";
        $result['status'] = 'n';
        $result['info'] = '文件类型不对';
        exit(json_encode($result));
    }
}
?>
